package pathways.Database.Specialized.Visual;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import pathways.CCompound;
import pathways.Database.Implementation.CDatabase;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;
import pathways.util.helper.CItemListModel;

public class CDBSVSynonyms extends JFrame
{
  JLabel lblCompound = new JLabel();
  JButton cmdSelectMasterCmpd = new JButton();
  JScrollPane scrollPaneSynonyms = new JScrollPane();
  JList lstSynonyms = new JList();
  JButton cmdAdd = new JButton();
  JLabel lblSynonyms = new JLabel();
  JButton cmdSave = new JButton();
  JButton cmdClose = new JButton();
  
  CCompound mMasterCmpd = null;
  CItemListModel mdllstSynonyms = new CItemListModel(new Vector());
  CDatabase db = null;
  
  public CDBSVSynonyms(CDatabase db)
  {
    this();
    
    this.db = db;
    
    setSize(440,375);  
  }
  
  private CDBSVSynonyms()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  
  private void jbInit() throws Exception
  {
    lblCompound.setFont(new java.awt.Font("Dialog", 1, 12));
    lblCompound.setText("Compound:");
    lblCompound.setBounds(new Rectangle(6, 10, 397, 23));
    this.getContentPane().setLayout(null);
    cmdSelectMasterCmpd.setText("Select");
    cmdSelectMasterCmpd.setBounds(new Rectangle(322, 41, 90, 28));
    cmdSelectMasterCmpd.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSelectMasterCmpd_actionPerformed(e);
      }
    });
    scrollPaneSynonyms.setBounds(new Rectangle(16, 101, 295, 199));
    cmdAdd.setBounds(new Rectangle(322, 101, 90, 28));
    cmdAdd.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdAdd_actionPerformed(e);
      }
    });
    cmdAdd.setText("Add");
    lblSynonyms.setText("Synonyms:");
    lblSynonyms.setBounds(new Rectangle(17, 76, 123, 21));
    cmdSave.setText("Save");
    cmdSave.setBounds(new Rectangle(67, 313, 90, 28));
    cmdSave.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSave_actionPerformed(e);
      }
    });
    cmdClose.setText("Close");
    cmdClose.setBounds(new Rectangle(179, 313, 90, 28));
    cmdClose.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdClose_actionPerformed(e);
      }
    });
    this.getContentPane().add(lblCompound, null);
    this.getContentPane().add(scrollPaneSynonyms, null);
    scrollPaneSynonyms.getViewport().add(lstSynonyms, null);
    this.getContentPane().add(cmdAdd, null);
    this.getContentPane().add(cmdSelectMasterCmpd, null);
    this.getContentPane().add(lblSynonyms, null);
    this.getContentPane().add(cmdSave, null);
    this.getContentPane().add(cmdClose, null);
  }

  void cmdSelectMasterCmpd_actionPerformed(ActionEvent e)
  {
    CCompound cmpd = searchCompound();
    
    if (cmpd != null)
    {
      mMasterCmpd = cmpd;
      this.lblCompound.setText("Compound: " + cmpd.getName());
      
      ResultSet rs = db.getSelect().getSynonyms((int)cmpd.getID(),cmpd.getName());
      
      this.mdllstSynonyms.getVector().removeAllElements();
      
      try {
	while(rs.next())
	{
	  CItem item = new CItem(rs.getInt(1),rs.getString(2));
	
	  this.mdllstSynonyms.getVector().add(item);
	}
      } catch (SQLException sqle) {
	CDebug.println(this,"cmdSelectMasterCmpd_actionPerformed. SQLException: " + sqle.toString());
      }
      
      this.lstSynonyms.setModel(this.mdllstSynonyms);      
    }
  }
  
  private CCompound searchCompound()
  {
    CSearchCompound c = new CSearchCompound(true,this,db,CSearchCompound.FILTER_NONE);
    
    c.show();
    
    return c.getSelectedCompound();
  }
  
  void cmdAdd_actionPerformed(ActionEvent e)
  {
    CCompound cmpd = searchCompound();
    
    if (cmpd != null)
    {
      CItem item = new CItem((int)cmpd.getID(),cmpd.getName());
      
      if (!alreadyExists(item))
      {    
    	addItem(item);
      }
      else
      {
    	JOptionPane.showMessageDialog(this,"Already exists.","",JOptionPane.INFORMATION_MESSAGE);
      }    
    }
  }
  
  private boolean alreadyExists(CItem newItem)
  {
    Vector v = this.mdllstSynonyms.getVector();
    
    for (int i = 0; i<v.size(); i++)
    {
      CItem item = (CItem)v.get(i);
      
      if (item.equals(newItem))
	return true;  // exists
    }  
    
    return false; // does not exist
  }
  
  void cmdSave_actionPerformed(ActionEvent e)
  {
    
    if (this.mMasterCmpd == null)
    {
      JOptionPane.showMessageDialog(this,"Please select a Compound first.","",JOptionPane.ERROR_MESSAGE);
      return;  
    }
    
    Vector v = this.mdllstSynonyms.getVector();
    
    try{
      db.getConnection().beginTrans();
    } catch (SQLException sqle) {}
    
    for (int i = 0; i<v.size(); i++)
    {
      CItem item = (CItem)v.get(i);
      
      db.getUpdate().newSynonymn((int)mMasterCmpd.getID(),item.getID());
    }  
    
    try {
      db.getConnection().commitTrans();
    } catch (SQLException sqle) {}
    
    JOptionPane.showMessageDialog(this,"Saved successfully","Saved Successfully",JOptionPane.INFORMATION_MESSAGE);
    
    this.dispose();
  }

  void cmdClose_actionPerformed(ActionEvent e)
  {
    this.dispose();
  }  
  
  void addItem(CItem item)
  {
    Vector v = new Vector();
    
    v.addAll(this.mdllstSynonyms.getVector());
    
    v.add(item);
    
    this.mdllstSynonyms = new CItemListModel(v);
    this.lstSynonyms.setModel(this.mdllstSynonyms);
  }
  
}